package com.ht.medicaltreatment.serial

import com.blankj.utilcode.util.LogUtils
import com.gym.baselib.utils.runOnMain
import com.ht.medicaltreatment.utils.ByteUtil
import com.ht.medicaltreatment.utils.CRCUtils
import com.ht.medicaltreatment.utils.UIDUitls
import com.kongqw.serialportlibrary.SerialPortFinder
import com.kongqw.serialportlibrary.SerialPortManager
import com.kongqw.serialportlibrary.listener.OnOpenSerialPortListener
import com.kongqw.serialportlibrary.listener.OnSerialPortDataListener
import com.xgst.lib.utils.TimeUtils
import java.io.File

/**
 * 3A0400400382 盘存HF
 * 3A030007C1   运行状态      3a090007 [00 01] 00000000ca
 * 3a03004284   获取HF数据:   3a0b0042 [6caf44a9500104e0] 17
 * 3a030008ce   切换上位机类型
 *
 * 3A04003003F2  盘存UHF
 * 3A030032F4    获取UHF数据  3a 13 00 32 【行02  列00】 29 00 【300833b2ddd9014000000037】 04
 */
object SerialPortUtils {
    lateinit var mSerialPortManager: SerialPortManager
    private lateinit var saveUHF:ByteArray
    private lateinit var saveHF:ByteArray
    private lateinit var stopUHF:ByteArray
    private lateinit var readUHF:ByteArray
    private lateinit var readHF:ByteArray
    private lateinit var stateUHF:ByteArray
    private lateinit var chooseAndroidState:ByteArray
    private var isUHF = true

    private var onReceive:(String,Int,Int)->Unit = {uid,b,c->}
    /**
     * 初始化设备列表
     */
    fun initDevice() {
        val serialPortFinder = SerialPortFinder()
        val devices = serialPortFinder.devices
        if (devices == null || devices.size == 0) {
            LogUtils.i("获取不到串口设备")
            return
        }
        mSerialPortManager = SerialPortManager()

        //打开串口的监听
        mSerialPortManager.setOnOpenSerialPortListener(object : OnOpenSerialPortListener {
            override fun onSuccess(device: File?) {
                LogUtils.i("打开串口成功${device?.absoluteFile}")
            }

            override fun onFail(device: File?, status: OnOpenSerialPortListener.Status?) {
                when(status){
                    OnOpenSerialPortListener.Status.NO_READ_WRITE_PERMISSION -> LogUtils.i("没有读写权限")
                    else -> LogUtils.i("打开串口失败")
                }
            }
        })

        //串口数据通信监听
        mSerialPortManager.setOnSerialPortDataListener(object : OnSerialPortDataListener {
            override fun onDataReceived(bytes: ByteArray) {
                //val command = ByteUtil.bytes2HexStr(bytes)
                runOnMain {
                    processingData(bytes)
                }
            }

            override fun onDataSent(bytes: ByteArray) {
                LogUtils.i("发送的指令:${ByteUtil.bytes2HexStr(bytes)}")
            }
        })

        mSerialPortManager.openSerialPort(File("/dev/ttyS4"), 115200)

        /*//遍历串口
        devices.forEach {
            LogUtils.i(it.file)
            if(it.name.contains("ttyS4")){
                val open = mSerialPortManager.openSerialPort(it.file, 115200)
                LogUtils.i("打开串口$open")
            }
        }*/

        //运行状体
        val ChooseState = byteArrayOf(0x3A.toByte(), 0x03.toByte(), 0x00.toByte(), 0x07.toByte())
        chooseAndroidState = CRCUtils.byteAddToByte(ChooseState,CRCUtils.getCrcData(ChooseState))

        //运行状体
        val UHFState = byteArrayOf(0x3A.toByte(), 0x03.toByte(), 0x00.toByte(), 0x07.toByte())
        stateUHF = CRCUtils.byteAddToByte(UHFState,CRCUtils.getCrcData(UHFState))

        //开始盘存超高频
        val UHFSave = byteArrayOf(0x3A.toByte(), 0x04.toByte(), 0x00.toByte(), 0x30.toByte(), 0x03.toByte())
        saveUHF = CRCUtils.byteAddToByte(UHFSave,CRCUtils.getCrcData(UHFSave))
        //开始盘存高频
        val HFSave = byteArrayOf(0x3A.toByte(), 0x04.toByte(), 0x00.toByte(), 0x40.toByte(), 0x03.toByte())
        saveHF = CRCUtils.byteAddToByte(HFSave,CRCUtils.getCrcData(HFSave))
        //停止盘存
        val UHFStop = byteArrayOf(0x3A.toByte(), 0x03.toByte(), 0x00.toByte(), 0x31.toByte())
        stopUHF = CRCUtils.byteAddToByte(UHFStop,CRCUtils.getCrcData(UHFStop))
        //读取UHF盘存
        val UHFRead = byteArrayOf(0x3A.toByte(), 0x03.toByte(), 0x00.toByte(), 0x32.toByte())
        readUHF = CRCUtils.byteAddToByte(UHFRead,CRCUtils.getCrcData(UHFRead))

        //读取HF盘存
        val HFRead = byteArrayOf(0x3A.toByte(), 0x03.toByte(), 0x00.toByte(), 0x42.toByte())
        readHF = CRCUtils.byteAddToByte(HFRead,CRCUtils.getCrcData(HFRead))
    }

    private fun processingData(bytes: ByteArray) {
        val data = CRCUtils.bytes2HexString(bytes)
        LogUtils.i("接收到的指令:$data")
        if(bytes.size >= 5 && bytes[0] == 0x3A.toByte() && bytes[1] == 0x09.toByte()&& bytes[3] == 0x07.toByte()){
            //获取运行状态 3a 09 00 07 [00 00 00 01 00 00] ca
            if(isUHF){
                if(bytes[4] == 0x01.toByte()){
                    //UHF有值， 获取数据
                    readUHF()
                }else if(bytes[4] == 0x00.toByte()){
                    onReceive("",0,0)
                }
            }else{
                if(bytes[5] == 0x01.toByte()){
                    //HF有值，获取数据
                    readHF()
                }else if(bytes[5] == 0x00.toByte()){
                    onReceive("",0,0)
                }
            }
        }else if(bytes.size > 10 && bytes[0] == 0x3A.toByte() && bytes[1] == 0x13.toByte()&& bytes[3] == 0x32.toByte()){
            //获取UHF盘存数据  3a 13 00 32  [02 01 3a 30]  [30 08 33 b2 dd d9 01 40 00 00 00 12] 03
            //行
            val row = bytes[4].toInt()
            //列
            val col = bytes[5].toInt()
            val uid = data.substring(16,data.length-2)
            onReceive(uid,row,col)
            //检查是否还有数据
            checkData()
        }else if(bytes.size > 10 && bytes[0] == 0x3A.toByte() && bytes[3] == 0x42.toByte()){
            //获取HF盘存数据  3a0b0042 6caf44a9500104e0 17
            var uid = data.substring(7,data.length-2)
            uid = UIDUitls.sort(uid)
            onReceive(uid,0,0)
            //检查是否还有数据
            checkData()
        }
    }

    /**
     * 超高频
     */
    fun startUHF(){
        isUHF = true
        mSerialPortManager.sendBytes(saveUHF)
        TimeUtils.countDownOneTime(500,{
            checkData()
        })
    }

    /**
     * 高频
     */
    fun startHF(){
        isUHF = false
        mSerialPortManager.sendBytes(saveHF)
        TimeUtils.countDownOneTime(1000,{
            checkData()
        })
    }

    fun stopUHF(){
        mSerialPortManager.sendBytes(stopUHF)
    }

    fun readUHF(){
        mSerialPortManager.sendBytes(readUHF)
    }

    fun readHF(){
        mSerialPortManager.sendBytes(readHF)
    }

    fun checkData(){
        mSerialPortManager.sendBytes(stateUHF)
    }

    fun setListener(onReceive:(String,Int,Int)->Unit){
        this.onReceive = onReceive
    }

    /**
     * 关闭串口
     */
    fun close(){
        mSerialPortManager.closeSerialPort()
    }

    fun open(){
        mSerialPortManager.openSerialPort(File("/dev/ttyS4"), 115200)
    }
}